<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>Test</title>
<script language="JavaScript" src="../jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" src="../simple-template.js"></script>
<script type="text/javascript">
var t;
var t2;
var t3 = function(options){
	return new SimpleTemplate(options);
}
function $(id){
	return document.getElementById(id);
}
function assertError(expected, fn){
	try{
		fn();
	}catch(e){
		assertEquals(expected, e);
		return;
	}
	fail("no error");
}
function assertErrorMessage(expected, fn){
	try{
		fn();
	}catch(e){
		assertEquals(expected, e.message);
		return;
	}
	fail("no error");
}
function setUp(){
	t = new SimpleTemplate({input:"html"});
	t2 = new SimpleTemplate();
}
function tearDown(){
}
function testAddon(){
	assertEquals("$valu<e$", t.parse('$$# null@default:"valu&lt;e" #$'));
	assertEquals("$$", t.parse('$$# nullx@default:"value"@ignoreError #$'));
	assertEquals("$value$", t.parse('$$# null@default:"value"@ignoreError #$'));
	assertEquals("$v3$", t.parse('$$# nullx@default:"value"@ignoreError@default:"v"+ 3 #$'));

	assertEquals("$true$", t.parse('$$# 1 &lt; 3 @default:"value",1,4,8 #$'));
	var fn = function(target, v1, v2, v3){
		return target + v1 + v2 + v3;
	};
	fn["catch"] = function(e, v1, v2, v3){
		return v1 + v3 + v2;
	}
	assertEquals("10value340", t.parse('$# a@default:"value",b,40,9,7, #', {a:10,b:3}, {addon:{"default":fn}}));
	assertEquals("10value340", t3({addon:{"default":fn}, input:"html"}).parse('$# a@default:"value",b,40,9,7, #', {a:10,b:3}));
	assertEquals("10value340", t3({addon:{"default":$}, input:"html"}).parse('$# a@default:"value",b,40,9,7, #', {a:10,b:3}, {addon:{"default":fn}}));
	
	assertEquals("value403", t.parse('$# c@default:"value",b,40,9,7, #', {a:10,b:3}, {addon:{"default":fn}}));
	assertEquals("value403", t3({addon:{"default":fn}, input:"html"}).parse('$# c@default:"value",b,40,9,7, #', {a:10,b:3}));
	assertEquals("value403", t3({addon:{"default":$}, input:"html"}).parse('$# c@default:"value",b,40,9,7, #', {a:10,b:3}, {addon:{"default":fn}}));
}
function testUnescape(){
	assertEquals('$&lt;&gt;&amp;&quot;<>&"<>&<>&$', t.parse('$&lt;&gt;&amp;&quot;<>&"$# &quot;&lt;&gt;&amp;<>&" #$'));
	assertEquals('$&lt;&gt;&amp;&quot;<>&"<>&<>&$', t3({input:"html",unescape:"html"}).parse('$&lt;&gt;&amp;&quot;<>&"$# &quot;&lt;&gt;&amp;<>&" #$',null,{unescape:"template"}));

	assertEquals('$<>&"<>&"<>&<>&$', t.parse('$&lt;&gt;&amp;&quot;<>&"$# &quot;&lt;&gt;&amp;<>&" #$',null,{unescape:"html"}));
	assertEquals('$<>&"<>&"<>&<>&$', t3({input:"html",unescape:"template"}).parse('$&lt;&gt;&amp;&quot;<>&"$# &quot;&lt;&gt;&amp;<>&" #$',null,{unescape:"html"}));
	assertEquals('$<>&"<>&"<>&<>&$', t3({input:"html",unescape:"html"}).parse('$&lt;&gt;&amp;&quot;<>&"$# &quot;&lt;&gt;&amp;<>&" #$'));
	assertEquals('$<>&"<>&"<>&<>&$', t3({input:"html",unescape:"html"}).parse('$&lt;&gt;&amp;&quot;<>&"$# &quot;&lt;&gt;&amp;<>&" #$',null,{unescape:"tem"}));
}
function testReadElement(){
	assertEquals("!45!46!", t2.parse('template-id',{first:1}));
	t2.readAndRemoveElement('template-id');
	assertEquals("!45!50!", t2.parse('template-id',{first:5}));
	assertNull(document.getElementById('template-id'));
	assertEquals("!45!52!", t2.parse('template-id',{first:7}));
}
function testReadElement2(){
	assertEquals("test63", t2.parse('template-id2',{value:7}));
	t2.readElementAndRemoveChildren('template-id2');
	assertEquals("test81", t2.parse('template-id2',{value:9}));
	assertEquals(0, document.getElementById('template-id2').childNodes.length);
}
function testReadElement3(){
	var doc = document.getElementById("if2").contentWindow.document;
	assertEquals("!55!56!", t2.parse('template-idClone',{first:1},{document:doc}));
	t2.readAndRemoveElement('template-idClone',doc);
	assertEquals("!45!50!", t2.parse('template-idClone',{first:5}));
	assertEquals("!55!60!", t2.parse('template-idClone',{first:5},{document:doc}));
	assertNotNull(document.getElementById('template-idClone'));
	assertNull(doc.getElementById('template-idClone'));
	assertEquals("!45!52!", t2.parse('template-idClone',{first:7}));
	assertEquals("!55!64!", t2.parse('template-idClone',{first:9},{document:doc}));
}
function testReadElement4(){
	var doc = document.getElementById("if2").contentWindow.document;
	
	assertEquals("test693", t2.parse('template-id2Clone',{value:7},{document:doc}));
	t2.readElementAndRemoveChildren('template-id2Clone',doc);
	assertEquals("test81", t2.parse('template-id2Clone',{value:9}));
	assertEquals("test891", t2.parse('template-id2Clone',{value:9},{document:doc}));
	assertNotEquals(0, document.getElementById('template-id2Clone').childNodes.length);
	assertEquals(0, doc.getElementById('template-id2Clone').childNodes.length);
}
</script>
<body>
<div id="template-id">!$# 
var val = 0;
for(var i = 0; i < 10; i++){
	val += i;
}
val;
@default:"value" #!$# @default:function(){
var val = first;
for(var i = 0; i < 10; i++){
	val += i;
}
return val;
}()
,"test"  #!</div>
<div id="template-id2">test$# 9*value #</div>
<div id="template-idClone">!$# 
var val = 0;
for(var i = 0; i < 10; i++){
	val += i;
}
val;
@default:"value" #!$# @default:function(){
var val = first;
for(var i = 0; i < 10; i++){
	val += i;
}
return val;
}()
,"test"  #!</div>
<div id="template-id2Clone">test$# 9*value #</div>
<iframe id="if2" src="iframe2.html" style="display:none"></iframe>
</body>
</html>